﻿using lwzlz.Core;
using lwzlz.Core.Repository;
using System;
using System.Collections.Generic;

namespace lwzlz.Application.System
{
    public class RoleService
    {
        private readonly IRepository<Role> _repository;

        public RoleService()
        {
            _repository = new Repository<Role>();
        }
        #region 获取角色
        public IEnumerable<Role> GetList()
        {
            return _repository.GetList("where IsDeleted=0 and IsEnable=1");
        }
        #endregion

        #region 获取角色
        public Role Get(int id)
        {
            return _repository.Get(id);
        }
        #endregion

        #region 添加
        public int? Add(Role model)
        {
            return _repository.Insert(model);
        }
        #endregion

        #region 更新
        public int UpDataSave(Role model)
        {
            return _repository.Update(model);
        }
        #endregion

        #region 获得分页数据
        public IEnumerable<dynamic> GetDynamicPaged(int pageNumber, int rowsPerPage, string orderby, string sql, object param = null)
        {
            return _repository.GetDynamicPaged(pageNumber, rowsPerPage, orderby, sql, param);
        }
        public int RecordQueryCount(string sql, object param)
        {
            return _repository.RecordQueryCount(sql, param);
        }
        #endregion

        #region 批量删除
        public void DeleteList(string IdList)
        {
            var dbtran = _repository.BeginTransaction();//开启事务
            try
            {
                foreach (string Id in IdList.Split(','))
                {
                    Role model = _repository.Get(Id, dbtran);
                    model.IsDeleted = true;
                    model.DeletionTime = DateTime.Now;
                    model.DeleterUserId = UserAuthentication.GetCurrentUser().UserID;

                    _repository.Update(model, dbtran);
                }
                dbtran.Commit();//提交事务
            }
            catch (Exception)
            {
                dbtran.Rollback();//事务回滚
            }
            finally
            {
                dbtran.Dispose();//释放事务资源
            }
        }
        #endregion

        public IEnumerable<Role> GetList(string where)
        {
            return _repository.GetList(where);
        }
    }
}
